<?php
namespace My\Controller;

use Utuo\BackgroundController;

class ActivityScratchController extends BackgroundController {
	
	private $status = array(
			'0' => '关闭',
			'1' => '开启'
	);
	
	private $model = null;
	
	public function _initialize() {
		$this->model = I("model", "");
		$this->setControllerTitle("活动管理");
		$this->setActionTitle(array(
				'index' => '刮刮卡活动列表',
				'modify' => '刮刮卡活动修改',
				'wechat' => '微信回复设置',
				'recycle' => '回收站'
		));
		$this->setActionTitle("add", "刮刮卡活动添加", array('model'=>I("model")));
		$this->setActionNav("index,add,recycle");
	}
	
	// 活动类型列表
	public function indexAction() {
		$this->getScratchActivityList(0);
	}
	
	// 回收站
	public function recycleAction() {
		$this->getScratchActivityList(1);
	}
	
	
	// 活动添加
	public function addAction() {
		$activityScratch = array();
		$activityScratch['statusRadio'] = $this->getRadio("status", $this->status, 1);
		$activityScratch['begin_time'] = time();
		$activityScratch['end_time'] = strtotime("+3 day");
		$this->assign("prizeConfigJSON", json_encode(array()));
		$this->assign("activityScratch", $activityScratch);
		$this->display("save");
	}
	
	// 活动修改
	public function modifyAction() {
		$activityScratch = $this->getOneById("ActivityScratch");
		$activityScratch['statusRadio'] = $this->getRadio("status", $this->status, $activityScratch['status']);
		$activityScratch['prize_config'] = unserialize($activityScratch['prize_config']);
		$this->assign("prizeConfigJSON", json_encode($activityScratch['prize_config'] ));
		$this->assign("activityScratch", $activityScratch);
		$this->display("save");
	}
	
	// 活动保存
	public function savePostAction() {
		$d = D("ActivityScratch");
		if($d->create()) {
			if(!empty($d->id)){
				unset($d->model);
				$result = $d->save();
			}else {
				$d->cid = $this->aid;
				$d->model_id = M('Model')->where("tag='%s'", $this->model)->getField('id') or $this->ajaxError("未设置模型");
				$result = $d->add();
			}
		}
		$result !== false and $this->ajaxSuccess("保存成功");
		$this->ajaxError("保存失败");
	}
	
	// 微信回复设置
	public function wechatAction() {
		$id = I("id", 0, "intval");
		$activityScratch = M("ActivityScratch")->find($id);
		$wechat_rule = '__scratch_rule__';
		$d = D('WechatReply');
		$reply = $d->getReply($this->aid, $wechat_rule);
		if (!$reply) {
			$url = url('Home', 'ActivityScratch', 'show', 'sid=' . $this->aid.'&id='.$id, true);
			$id = $d->setKeywordReply($this->aid, $wechat_rule, '刮刮卡', $activityScratch["name"], $activityScratch["intro"], $activityScratch["pic"], $url);
			$id && $reply = $d->find($id);
		}
		$reply['keyword'] = trim($reply['keyword'], ' ,');
		$this->assign('reply', $reply);
		$this->display();
	}
	
	// 提交微信回复
	public function wechatPostAction() {
		$d = D('WechatReply');
		if ($d->create()) {
			$d->keyword = $d->parseKeyword($d->keyword);
			if ($d->save()) {
				$this->ajaxSuccess();
			}
		}
		$this->ajaxError();
	}
	
	// 设置活动状态
	public function setStatusPostAction() {
		$this->ajaxSetStatus("ActivityScratch");
	}
	
	// 移除活动
	public function removePostAction() {
		$this->ajaxRemove ( "ActivityScratch" );
	}
	
	// 恢复活动
	public function restorePostAction() {
		$this->ajaxRestore ( "ActivityScratch" );
	}
	
	// 彻底删除活动
	public function deletePostAction() {
		$this->ajaxDelete( "ActivityScratch" );
	}
	
	// 获取列表
	private function getScratchActivityList($isDeleted) {
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$pid = I('pid', 0, 'intval');
		$activityScratchLists = M('ActivityScratch')->where("cid=%d and is_deleted=%d", $this->aid, $isDeleted)->limit($pagesize)->page($page)->select();
		$activityCount = M('ActivityScratch')->where("cid=%d and is_deleted=%d", $this->aid, $isDeleted)->count();
		$this->assign('activityScratchLists', $activityScratchLists);
		$this->showPage($activityCount, $pagesize);
		$this->display();
	}
	
	
}